<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalParseCss.php 400 2011-01-04 21:17:00Z steen.rabol $
 * @package Pradoportal.Common
 *
 */

/**
 *
 * @package Pradoportal.Common
 */
/*
@example

	$oCSS	= new PortalParseCss();
	$oCSS->parseFile("style.css");
	echo $oCSS["body"]["font-family"];
	$oCSS->css["body"]["background-image"]="url(background2.gif)";
	$newcss_string=$oCSS->buildcss();
*/

class PortalParseCss
{
	// associative array containing css-tags $this->CSS["body"]["background-color"]
	private $css;

	// array containing all css-tags
	private $csstags;

	// CSS String
	private $cssstr;

	public function __construct()
	{
		/* Init */
		$this->css		= "";
		$this->csstags	= "";
		$this->cssstr	= "";
	}

	public function parseFile($filename)
	{
		if(file_exists($filename))
		{
			return $this->parse(file_get_contents($filename));
		}
		else
		{
			return null;
		}
	}

	public function parse($css_str)
	{
		/* Parse CSS to Array */
		$this->cssstr	= $css_str;
		$this->css		= "";
		$this->csstags	= "";

		$css_str	= preg_replace("/[\r\n]+/","",$css_str);
		$css_class	= explode("}", $css_str);

		while (list($key,$val) = each ($css_class))
		{
			$aCSSObj	= explode("{",$val);
			if(!$aCSSObj)
			{
				continue;
			}
			
			$cSel		= strtolower(trim($aCSSObj[0]));

			if ($cSel && !$this->IsCommentLine($cSel) && isset($aCSSObj[1]))
			{
				$this->csstags[]	= $cSel;
				$a					= explode(";",$aCSSObj[1]);
				while(list($key,$val0) = each ($a))
				{
					if(trim($val0))
					{
						$aCSSSub	= explode(":",$val0);
						$cAtt		= strtolower(trim($aCSSSub[0]));
						if (isset($aCSSSub[1]))
						{
							$aCSSItem[$cAtt]	= trim($aCSSSub[1]);
						}
					}
				}

				if ((isset($this->css[$cSel])) && ($this->css[$cSel]))
				{
					if(is_array($this->css[$cSel]) && is_array($aCSSItem))
						$aCSSItem	= array_merge($this->css[$cSel],$aCSSItem);
				}

				if(isset($aCSSItem))
				{
					$this->css[$cSel] = $aCSSItem;
					unset($aCSSItem);
				}
			}

			if (strstr($cSel ,","))
			{
				/* there is a comma - duplicate tag name and delete original tag */
				$aTags	= explode(",",$cSel);

				foreach($aTags as $key0 => $value0)
				{
					if(isset($this->css[$cSel]))
						$this->css[$value0] = $this->css[$cSel];
				}

				if(isset($this->css[$cSel]))
					unset($this->css[$cSel]);
			}
		}
		unset($css_str,$css_class,$aCSSSub,$aCSSItem,$aCSSObj);
		return $this->css;
	}

	public function IsCommentLine($line)
	{
		$t = trim($line);

		if(substr($t,0,2) === "/*")
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function buildcss()
	{
		/* Builds CSS on Base of Array */
		$this->cssstr	= "";
		foreach($this->css as $key0 => $value0)
		{
			$this->cssstr .= "$key0 {\n";
			foreach($this->css[$key0] as $key1 => $value1)
			{
				$this->cssstr .= "\t$key1:$value1;\n";
			}
			$this->cssstr .="}\n";
		}
		return ($this->cssstr);
	}
}

?>